<?php 
namespace Admin\Controller;
class ImgController extends IsloginController{
	function oper(){
		$img = M("Img");
		$num=$img->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$arr = $img->limit("$start,$pageSize")->select();
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
			
		}
		$this->assign('arr',$arr);
		$this->display();
	}
	function add(){
		$fArr = M("Fimg")->select();
		$this->assign('fArr',$fArr);
		$sArr = M("Simg")->select();
		$this->assign('sArr',$sArr);
		$jArr = M('Jimg')->select();
		$this->assign('jArr',$jArr);
		$this->display();
	}
	function tianjia(){
		$_POST['addtime']=time();
		$upload= new \Think\Upload();
		$upload->autoSub  =false;
        $upload->maxSize   =9999999999 ;
		$upload->mimes=array('image/png',"image/gif","image/jpeg","image/jpg");
		$upload->rootPath = './Public';
       	$upload->savePath = '/Upload/';
		$info=$upload->upload();
		if(!$info){
            $this->error($upload->getError());

		}else{
            foreach($info as $file) {

                //$file_mini='./Public/'.$file['savepath'].'mini/'.$file['savename'];
                $file_path="./Public/Upload/".$file['savePath'].$file['savename'];

                $file_mini="./Public/images/".$file['savePath'].$file['savename'];
                $water="./Public/images/water.png";
                $image = new \Think\Image();
                $image->open($file_path);
// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                $image->thumb(450, 450)->save($file_mini);
               $image->open($file_path)->water($water,\Think\Image::IMAGE_WATER_CENTER,70)->save($file_path);
            }
            $_POST['imagename']=$info['imagename']['savename'];
		}
		$re = M("img")->data($_POST)->add();
		if($re){
			$this->success("添加成功！",U("Img/oper"));
		}
	}
    function del(){
        $up = M("Img");
        $id=$_POST["id"];
        $img = $up->where("id=$id")->find();
        $file_path="./Public/Upload/".$img['imagename'];
        $file_mini="./Public/images/".$img['imagename'];
        unlink($file_path);
        unlink($file_mini);
        $re = $up->where("id=$id")->delete();

        if($re){
            echo json_encode(array('msg'=>1));

        }else{

        }
    }
    function update(){
	    $upOb = M("Img");
	    $id=$_GET["id"];
	    $sOb = M("Simg");
	    $arr1 = $sOb->select();
	    $this->assign('arr1',$arr1);
	    $fOb = M("Fimg");
	    $arr2 = $fOb->select();
	    $this->assign('arr2',$arr2);
	    $arr = $upOb->where("id=$id")->find();
	    $this->assign('arr',$arr);
	    $this->display();
    }
    function xiugai(){
	    $xiugaiOb = M("Img");
	    $_POST["addtime"]=time();
	    $id=$_POST["id"];
        $arr = $xiugaiOb->where("id=$id")->find();
        $file_path="./Public/Upload/".$arr['imagename'];
        $file_mini="./Public/images/".$arr['imagename'];
        unlink($file_path);
        unlink($file_mini);
	    $re = $xiugaiOb->data($_POST)->save();
	    if($re){
            $this->success("修改成功！",U("Img/oper"));
        }else{
            $this->error("修改失败！",U("Img/oper"));
        }
    }
    function lingqv(){

    }
}